<%-- 
    Document   : main
    Created on : 23/09/2014, 22:20:02
    Author     : leoomoreira
--%>

<%@page import="cronos.applicationmetric.ApplicationMetric"%>
<%@page import="cronos.metric.Metric"%>
<%@page import="cronos.application.Application"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="../header.jsp" %>
<div id="title">Application Metric Form</div>
<% if (request.getAttribute("message") != null) {%>
<br/>
<div id="message"><%=request.getAttribute("message").toString()%></div>
<% } %>
<br/>
<form action="ApplicationMetricInsert" method="post" id="formInsert">
    <div class="table">
        <% List<Application> applicationList = (List<Application>) request.getAttribute("applicationList"); %>
        <% if (applicationList != null && applicationList.size() > 0) { %>
        <div class="row">
            <div class="label">Application:</div>
            <div class="field">
                <select name="applicationId" id="applicationId" onchange="if (this.value !== '') { document.location = 'ApplicationMetricList?applicationId=' + this.value; } else { document.location = 'ApplicationMetricList'; }">
                    <option value="">Select...</option>
                    <% for (Application a : applicationList) {%>
                    <option value="<%=a.getId()%>"<%= (request.getAttribute("applicationId") != null && Long.parseLong(request.getAttribute("applicationId").toString()) == a.getId() ? "selected=\"selected\"" : "")%>><%=a.getName()%></option>
                    <%} %>
                </select>
            </div>
        </div>
        <%}%>
        <% List<Metric> metricList = (List<Metric>) request.getAttribute("metricList"); %>
        <% if (metricList != null && metricList.size() > 0) { %>
        <div class="row">
            <div class="label">Metric:</div>
            <div class="field">
                <select name="metricId" id="metricId">
                    <% for (Metric m : metricList) {%>
                    <option value="<%=m.getId()%>"><%=m.getName()%></option>
                    <%} %>
                </select>
            </div>
        </div>
        <%}%>
    </div>
    <div>
        <button type="button" class="save" onclick="if (document.getElementById('applicationId') != null && document.getElementById('applicationId').value != '' && document.getElementById('metricId') != null && document.getElementById('metricId').value != '') { document.getElementById('formInsert').submit(); } else { alert('Complete all required fields'); }">
            <img src="images/save.png" title="Save" />Save
        </button>
    </div>
</form>
<%
    if (request.getAttribute("applicationMetricList") != null) {
%>
<br/>
<table class="list">
    <tr>
        <td class="caption">Id</td>
        <td class="caption">Name</td>
        <td class="twoopt">&nbsp;</td>
    </tr>
    <%
        List<Metric> result = (List<Metric>) request.getAttribute("applicationMetricList");
        for (int i = 0; result != null && i < result.size(); i++) {
            Metric item = result.get(i);
    %>
    <tr class="<%=(i % 2 == 0 ? "even" : "odd")%>">
        <td><%= item.getId()%></td>
        <td><%= item.getName()%></td>
        <td>
            <a href="ApplicationMetricDelete?applicationId=<%= request.getAttribute("applicationId") != null ? request.getAttribute("applicationId").toString() : ""%>&metricId=<%= item.getId()%>"><img src="images/delete.png" title="Delete" /></a>
        </td>
    </tr>
    <%
        }
    %>
</table>
<%
    }
%>
<%@include file="../footer.jsp" %>